package com.dingxiang.mobile.keyboard.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dingxiang.mobile.keyboard.api.DXKeyboardManager;
import com.dingxiang.mobile.keyboard.api.IDXKeyboardListener;
import com.dingxiang.mobile.keyboard.api.view.DXSafeEditView;

public class MainActivity extends Activity {
    private static final String TAG = "DXKeyboardDemo";

    private DXSafeEditView mDXEditText1, mDXEditText2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        /**
         * API方式调用顶象键盘
         */
        byApi();

        /**
         * EditText方式调用顶象键盘
         */
        byEditText();

    }

    /**
     * API方式调用顶象键盘
     */
    public void byApi(){
        View rootView = findViewById(R.id.sv_main);


        /**
         * 初始化
         */
        DXKeyboardManager.getInstance().init(this);

        /**
         * 防止输入控件被键盘遮盖
         */
       DXKeyboardManager.getInstance().setAdjustRootView(rootView);

        /**
         * 输入内容监听
         */
        DXKeyboardManager.getInstance().setListener(new IDXKeyboardListener() {
            @Override
            public void onTextChange(CharSequence str) {
                Log.d(TAG, "onTextChange " + str);
                Toast.makeText(MainActivity.this, "文本："+str, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShow() {
                Log.d(TAG, "onShow");
                Toast.makeText(MainActivity.this, "顶象键盘show", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onHide() {
                Log.d(TAG, "onHide");
                Toast.makeText(MainActivity.this, "顶象键盘hide", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClickFinish() {
                Log.d(TAG, "onClickFinish");
                Toast.makeText(MainActivity.this, "顶象键盘finish", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onKbHeightChange(int height) {

            }
        });


        /**
         * 打开键盘
         */
        findViewById(R.id.btn_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 默认为字母键盘
                 * DXKeyboardType.NORMAL 指定字母键盘
                 * DXKeyboardType.NUMBER 指定数字键盘
                 * DXKeyboardType.NUMBER_ONLY 指定数字键盘（无法切换其他模式）
                 */
                DXKeyboardManager.getInstance().show(MainActivity.this);
            }
        });


        /**
         * 隐藏键盘
         */
        findViewById(R.id.btn_hide).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DXKeyboardManager.getInstance().hide();
            }
        });




        /**
         * 清空输入数据
         */
        findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DXKeyboardManager.getInstance().clearContent();
            }
        });

        DXKeyboardManager.getInstance().show(MainActivity.this);
    }


    /**
     * EditText方式调用顶象键盘
     */
    public void byEditText(){

        mDXEditText1 = findViewById(R.id.special_ed);
        mDXEditText2 = findViewById(R.id.special_et);

        /**
         * 防止输入控件被键盘遮盖
         */
        View root_view = findViewById(R.id.sv_main);
        mDXEditText2.setAdjustRootView(root_view);

        /**
         * 显示第一个控件明文
         */
        findViewById(R.id.btn_show_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, mDXEditText1.getEditString(), Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * 显示第二个控件明文
         */
        findViewById(R.id.btn_show_data2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, mDXEditText2.getEditString(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * H5调用顶象键盘
     * @param view
     */
    public void goWeb(View view) {
        Intent intent = new Intent(this , MainActivity.class);
        startActivity(intent);
    }

    /**
     * H5调用顶象键盘
     * @param view
     */
    DXDialog dialog;
    public void goDialog(View view) {
        if (dialog == null) {
            dialog = new DXDialog(this);
        }
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        /**
         * 销毁键盘
         */
//        DXKeyboardManager.getInstance().destroy();
    }
}


